package phonedata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class FlowMain {
    public static void main(String[] args) throws Exception {
        // 1 配置对象
        Configuration conf = new Configuration();
        // 2 创建任务对象
        Job job = Job.getInstance(conf, "phone flow app");
        job.setJarByClass(FlowMain.class);

        // 2.1 设置 map和reduce任务类
        job.setMapperClass(FlowMap.class);
        job.setReducerClass(FlowReduce.class);
        //2.2 设置map和reduce 的输出KV
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        // 2.3 设置reduce的个数
        job.setNumReduceTasks(1);
        // 2.3 设置输入和输出路径
        String inFile = null;
        if (args != null && args.length > 0) {
            inFile = args[0];
        } else {
            String path = FlowMain.class.getResource("/").getPath();
            inFile = path + "/resources/HTTP_20130313143750.dat";
        }
        String outFile = inFile + "_" + System.currentTimeMillis() + ".out";
        FileInputFormat.setInputPaths(job, new Path(inFile));
        FileOutputFormat.setOutputPath(job, new Path(outFile));
        // 3 提交任务  等待程序执行完毕   返回值是否成功
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : -1);
    }
}
